<?php

/**
 * Description of Util_UiContentProvider
 *
 * @author Young Suk Ahn
 */
class Util_ProjectHelper extends Util_Helperbase{
	
	private $projectService;
	private $projectMemberService;
	
	private $profileHelper;

	public function __construct(&$request, $requestContext)
	{
		parent::__construct($request, $requestContext);
		$this->projectService = new Service_GenericSeedniaServiceStub(WSROOT.WSCONTEXT.'/'.SERVICE_PROJECT.'/', ACCESS_TOKEN);
		$this->projectService->init($requestContext);

		$this->projectMemberService = new Service_GenericSeedniaServiceStub(WSROOT.WSCONTEXT.'/'.SERVICE_PROJECT.'/member/', ACCESS_TOKEN);
		$this->projectMemberService->init($requestContext);

		$this->messageService = new Service_GenericSeedniaServiceStub(WSROOT.WSCONTEXT.'/'.SERVICE_MESSAGE.'/', ACCESS_TOKEN);
		$this->messageService->init($requestContext);

		//$this->profileHelper = Util_ProfileHelper
	}
	
	public function setProfileHelper($profileHelper)
	{
		$this->profileHelper = $profileHelper;
	}

	public function getProjectService()
	{
		return $this->projectService;
	}
	public function getProjectMemberService()
	{
		return $this->projectMemberService;
	}

	public function getProjectByXid($xid)
	{
		return $this->projectService->getByXid($xid);
	}
	public function getProjectList($criteria, $offset, $max)
	{
		return $this->projectService->getList($criteria, $offset, $max);
	}
	public function addProject($record)
	{
		return $this->projectService->add($record);
	}
	public function updateProject($record)
	{
		return $this->projectService->update($record);
	}
	
	public function addProjectMember($record)
	{
		return $this->projectMemberService->add($record, 'request/invite/');
	}
	
	public function loadProject(&$template, $projectSid = NULL)
	{	
		if ($projectSid == null)
			$projectSid = $this->request->query('projectSid');
		if ($projectSid == null)
			$projectSid = $this->_sessionInfo->currentProjectSid;
		if (isset($projectSid) && $projectSid > 0) {
			$project = $this->projectService->getByXid($projectSid);
			$template->set_global('project', $project);

			$this->loadSuggestedUsers($project, $template);
			return $project;
		}
		return null;
	}
	
	/**
	 *
	 * @param View $view the template
	 */
	public function loadUserProjects(&$template, $projectSid = NULL)
	{
		$userSid = $this->_sessionInfo->userInfo['userSid'];
		$projects = null;
		if ($userSid > 0) {
			
			// To get the list of projects the user is involve in
			$memberCriteria = array('userSid' => $userSid);
			$memberships = $this->projectMemberService->getList($memberCriteria, 0, 20, '');

			$projects = array();
			$projects['my goals'] = array();
			$projects['mentoring'] = array();
			$projects['collaborating'] = array();

			$currentProjectSid = $this->_sessionInfo->currentProjectSid;
			foreach ($memberships as $membership) {
				$project = new stdClass;
				$project->sid = $membership->projectSid;
				$project->name = $membership->projectName;
				if ($project->sid == $currentProjectSid) {
					$project->isActive = true;
				} else {
					$project->isActive = false;
				}
				if ($membership->type == "owner") {
					array_push($projects['my goals'],  $project);
				} else if ($membership->type == "mentor") {
					array_push($projects['mentoring'],  $project);
				} else if ($membership->type == "collab") {
					array_push($projects['collaborating'],  $project);
				}
			}

			$template->set_global('projects', $projects);
		}
		
		return $projects;
	}
	
	public function loadWaitingApproval(&$template)
	{
		$memberRequestCriteria = array('creatorSid' => $this->_sessionInfo->userInfo['userSid']);
		$memberRequestCriteria['status'] = 2; // join request
		$requested = $this->projectMemberService->getList($memberRequestCriteria, 0, 20, 'request/');
		
		$template->set_global('projectJoinRequested', $requested);
	}
	
	public function loadMembers($template, $projectSid)
	{
		$projectMemberService = new Service_GenericSeedniaServiceStub(WSROOT.WSCONTEXT.'/'.SERVICE_PROJECT.'/member/', 'ns1.MemberDto');
		$projectMemberService->setAccessToken('seedniaPhpUi');
		$projectMemberService->init($this->_sessionInfo);

		// Members
		if ($projectSid == null)
			$projectSid = $this->_sessionInfo->currentProjectSid;
		$memberCriteria = array('projectSid' => $projectSid);
		$memberList = $projectMemberService->getList($memberCriteria, 0, 20, '');

		$membersByType = array();
		foreach($memberList as $member)
		{
			$membersByType[$member->type][] =  $member;
		}
		$template->set_global('membersByType', $membersByType);
		$template->set_global('members', $memberList);
		
		return $memberList;
	}
	
	public function loadMemberRequests()
	{
		$memberRequestCriteria = array('projectSid' => $this->_sessionInfo->currentProjectSid);
		$memberRequestCriteria['status'] = 1; // invited
		$invited = $this->projectMemberService->getList($memberRequestCriteria, 0, 20, 'request/');
		
		$memberRequestCriteria = array('projectSid' => $this->_sessionInfo->currentProjectSid);
		$memberRequestCriteria['status'] = 2; // join request
		$requested = $this->projectMemberService->getList($memberRequestCriteria, 0, 20, 'request/');
		
		return array($invited, $requested);
	}

	
	public function loadProgress($template)
	{
		// Milestones
		$progressService = new Service_GenericSeedniaServiceStub(WSROOT.WSCONTEXT.'/'.SERVICE_PROGRESS.'/', 'ns1.ProgressDto');
		$progressService->setAccessToken('seedniaPhpUi');
		$progressService->init($this->_sessionInfo);

		$progressItemCriteria = array('projectSid' => $this->_sessionInfo->currentProjectSid);
		$progressItems = $progressService->getList($progressItemCriteria, 0, 20);
		$template->set_global('progressItems', $progressItems);
		
		return $progressItems;
	}

	public function loadSuggestedUsers($activeProject, &$template)
	{
		// Tasks
		$searchCriteria = array('skillTagNamesCsv'=> $activeProject->requiredSkillTagNamesCsv);
		$usersSearchResult = $this->profileHelper->getProfileService()->getList($searchCriteria, 0, 10, "search/");
		$suggestedUsers = array();
		// TODO: filter out the existing members
		foreach ($usersSearchResult->entries as $hit) {
			$suggestedUsers[] = $hit->record;
		}
		$template->set_global('suggestedUsers', $suggestedUsers);
		
		return $suggestedUsers;
	}
}
